package com.platform.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.platform.entity.ApiCircleBillVo;
import com.platform.entity.ApiCircleResVo;
import com.platform.entity.CircleEntity;
import com.platform.entity.UserEntity;
import com.platform.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-06-24 15:04:12
 */
@Controller
@RequestMapping("circle")
public class CircleController {
    @Autowired
    private UserService userService;
    @Autowired
    private CircleService circleService;
    @Autowired
    private ApiCircleResService circleResService;
    @Autowired
    private ApiCircleBillService circleBillService;


    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("circle:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<CircleEntity> apiCircleList = circleService.queryList(query);

        List<Map<String, Object>> list = new ArrayList<>();
        for (CircleEntity info : apiCircleList) {
            UserEntity userEntity = userService.queryObject(info.getUserId());

            Map<String, Object> map = new HashMap<>();
            map.put("id", info.getId());
            map.put("createTime", info.getCreateTime());
            map.put("userName", userEntity.getNickname());
            map.put("content", info.getContent());
            map.put("type", info.getType());
            list.add(map);
        }


        int total = circleService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("circle:info")
    @ResponseBody
    public R info(@PathVariable("id") Long id) {
        CircleEntity apiCircle = circleService.queryObject(id);
        UserEntity userEntity = userService.queryObject(apiCircle.getUserId());
        apiCircle.setUserName(userEntity.getNickname());

        Map<String, Object> params = new HashMap<>();
        params.put("circleId", id);
        /**
         * 资源
         */
        List<ApiCircleResVo> resVos = circleResService.selectList(params);

        /**
         * 跟进
         */
        List<ApiCircleBillVo> billVos = circleBillService.selectList(params);

        return R.ok().put("apiCircle", apiCircle).put("resVos", resVos).put("billVos", billVos);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("circle:save")
    @ResponseBody
    public R save(@RequestBody CircleEntity circle) {
        circleService.save(circle);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("circle:update")
    @ResponseBody
    public R update(@RequestBody CircleEntity circle) {
        circleService.update(circle);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("circle:delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids) {
        circleService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<CircleEntity> list = circleService.queryList(params);

        return R.ok().put("list", list);
    }
}
